import java.io.OutputStream;
import java.net.Socket;

//package com.rungeek.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.net.*;

public class ClientLogic {
    //Singleton
    private static ClientLogic single = null;
    protected ClientLogic() {
        //Creation -- protected, only run once
    }
    
    public static ClientLogic getInstance() {
        if(single==null) {
                single = new ClientLogic();
        }
        return single;
    }
   
    /**
     * @param args
     */
    public static void main(String[] args){
        // TODO Auto-generated method stub
    }
   
    public static boolean isFriend(String userName) throws SQLException {
        String myquery = "SELECT * FROM `Friends` WHERE User1 = '"+userName+"'";
        Statement stat;
        ResultSet rs = stat.executeQuery(myquery);
        boolean _check = false;
        while (rs.next()){
            if (rs.getString(0) == userName){
                _check = true;
                break;
            }              
        }l
        return _check;
    }
   
    private boolean exists(String username) {
        //dont need create tcp connection because this method is only called as sub method  
        //of other methods that need to create tcp connections to talk to the server
        // ask server to check if username is there
        //return whatever the server sends back
    	return true;
    }
     
    public boolean addFriend(String Username) throws SQLException {
        //create tcp connection with server
        if (exists(Username)) {
        //sends message to server to insert relation into Friends database
        //sends message to retrieve online status (could be done as part of message above)
        //add username to friend list
        //return true if successful, else false
        return true;
      }
      return false;
    }
       
    public boolean removeFriend(String Username) throws SQLException {
        //create tcp connection with server
        //sends message to server to remove relation from Friends database
        //remove username from friend list
        //close tcp connection
    	  //return true if successful, else false
    }
    
    public void createUser(String Username, String Password, String IP) throws SQLException {
        //create tcp connection with server
        if (!exists(Username)){
            //tell server to add User to database
            // create local database (don't need to ask server for friends since new account)
            //close tcp conn
            return true;
        }
        //close tcp conn
        //return true if successful, else false
        return false;
    }
    
    public boolean changePassword(String password) {
        //tell server to change password for this user
    	  //return true if successful, else false
        return true;
    }
       
    public void sendMessage(int clientID, String text) throws IOException {
        Socket sckt = new Socket();
        //TODO Grab socket from database
        sckt.connect(null);
        OutputStream sckt_stream = sckt.getOutputStream();
        sckt_stream.write(text.getBytes());
        sckt_stream.close();
    }
}


